_realname=qemu
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_base_ver="6.2.0"
# QEMU Versioning of RC-SourcePackage and RC-Version differs
# e.g. qemu-6.1.0-rc0.tar.xz contains 6.0.90, qemu-6.1.0-rc1.tar.xz contains 6.0.91
# Unset _rc_no to create Release-Build
#_rc_no="4"
_rc_base_ver="6.1.9${_rc_no}"
_rc_file_ver="rc${_rc_no}"
_tarname=$( [ -z "${_rc_no}" ] && echo ${_realname}-${_base_ver} || echo ${_realname}-${_base_ver}-${_rc_file_ver} )
pkgver=$( [ -z "${_rc_no}" ] && echo ${_base_ver} || echo ${_rc_base_ver} )
pkgrel=1
pkgdesc="QEMU - a generic and open source machine emulator and virtualizer (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=('GPL2' 'LGPL2')
url="https://qemu.org/"
options=('!emptydirs')
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-meson"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-python"
  "${MINGW_PACKAGE_PREFIX}-python-sphinx"
  "${MINGW_PACKAGE_PREFIX}-python-sphinx_rtd_theme"
  "${MINGW_PACKAGE_PREFIX}-autotools"
  "${MINGW_PACKAGE_PREFIX}-cc"
  'perl' 'bison' 'bsdtar')
depends=(
  "${MINGW_PACKAGE_PREFIX}-capstone"
  "${MINGW_PACKAGE_PREFIX}-curl"
  "${MINGW_PACKAGE_PREFIX}-cyrus-sasl"
  "${MINGW_PACKAGE_PREFIX}-glib2"
  "${MINGW_PACKAGE_PREFIX}-gnutls"
  "${MINGW_PACKAGE_PREFIX}-gtk3"
  "${MINGW_PACKAGE_PREFIX}-libjpeg"
  "${MINGW_PACKAGE_PREFIX}-libnfs"
  "${MINGW_PACKAGE_PREFIX}-libpng"
  "${MINGW_PACKAGE_PREFIX}-libslirp"
  "${MINGW_PACKAGE_PREFIX}-libssh"
  "${MINGW_PACKAGE_PREFIX}-libssp"
  "${MINGW_PACKAGE_PREFIX}-libtasn1"
  "${MINGW_PACKAGE_PREFIX}-libusb"
  "${MINGW_PACKAGE_PREFIX}-libxml2"
  "${MINGW_PACKAGE_PREFIX}-lzo2"
  "${MINGW_PACKAGE_PREFIX}-nettle"
  "${MINGW_PACKAGE_PREFIX}-pixman"
  "${MINGW_PACKAGE_PREFIX}-SDL2"
  "${MINGW_PACKAGE_PREFIX}-SDL2_image"
  "${MINGW_PACKAGE_PREFIX}-spice"
  "${MINGW_PACKAGE_PREFIX}-snappy"
  "${MINGW_PACKAGE_PREFIX}-usbredir"
  "${MINGW_PACKAGE_PREFIX}-zstd"
)
optdepends=(
  "${MINGW_PACKAGE_PREFIX}-gtk-vnc: Contains gvncviewer. For connections to Qemu VNC server"
  "${MINGW_PACKAGE_PREFIX}-spice-gtk: Contains spicy. For connections to Qemu SPICE server"
  "${MINGW_PACKAGE_PREFIX}-virt-viewer: Contains remote-viewer. For connections to Qemu SPICE or VNC server"
)
source=(
  "https://download.qemu.org/${_tarname}.tar.xz"{,.sig}
  msys2.readme.txt
  msys2.examples.tests.sh
)
sha256sums=(
  '68e15d8e45ac56326e0b9a4afa8b49a3dfe8aba3488221d098c84698bca65b45'
  'SKIP'
  '51625fd83c0a63729942d3dfc6d48be3491a700fe0f7cb8e88935b46081c9016'
  '75723aade171bfe92082262819624df726f30122ccdd3a194584a3b74320f35d'
)
validpgpkeys=('CEACC9E15534EBABB82D3FA03353C9CEF108B584') # Michael Roth <flukshun@gmail.com>
# tar cannot create links, to keep build running, manual extraction is required
noextract=(${_tarname}.tar.xz)

prepare() {
  [[ -d "${srcdir}"/${_tarname} ]] && rm -rf "${srcdir}"/${_tarname}
  # tar cannot create links here, therefore hide the failure
  tar -xf ${srcdir}/${_tarname}.tar.xz -C ${srcdir} || true

  cd "${srcdir}/${_tarname}"
}

build() {
  LDFLAGS+=" -fstack-protector"
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  # configure failes to create links, which can be ignored
  # qemu enables all features if possible, so keep it simple
  # For faster testing:
  #TARGETLIST="--target-list=x86_64-softmmu"
  ../${_tarname}/configure $TARGETLIST \
    --prefix=${MINGW_PREFIX} \
    --bindir=bin \
    --datadir=share/qemu \
    --localedir=share/locale \
    --mandir=share/man \
    --docdir=share/doc/qemu

  # Finally build
  make
}

check() {
  cd "${srcdir}"/build-${CARCH}
  # Tests are not performed in CI
  # For local testing: perform, but do not fail
  make test || true
}

package() {
  cd "${srcdir}"/build-${CARCH}

  # Install to ${pkgdir}
  make DESTDIR="${pkgdir}" install

  # ${MINGW_PREFIX} was installed somewhere below ${pkgdir}, determine ...
  local P_INSTALL=$(find "${pkgdir}" -name "$(basename ${MINGW_PREFIX})" -type d)
  # Move directly to ${pkgdir}, if not already there
  [ -d "${pkgdir}"/${MINGW_PREFIX} ] || mv -v "$P_INSTALL" "${pkgdir}"/

  # To enable usage relative paths for next operations
  cd "${pkgdir}"/${MINGW_PREFIX}

  # Move content to standard dirs in share/
  mkdir -pv share/qemu/icons
  mv -v share/qemu/icons share/
  mkdir -pv share/qemu/applications
  mv -v share/qemu/applications share/

  # Fix firmware descriptors
  # see https://bugzilla.redhat.com/show_bug.cgi?id=1728652#c7
  mkdir -pv share/qemu/firmware
  find share/qemu/firmware -type f -exec \
    sed -i "s%\(\"filename\"\s*:\s*\"\).*edk2%\1${MINGW_PREFIX}/share/qemu/edk2%" {} \;

  # Add all licenses found in qemu sources to share/licences/qemu/
  mkdir -pv share/licenses/qemu
  tar -C "${srcdir}"/${_tarname} -c $(
    cd "${srcdir}"/${_tarname} &&
      find -iname "*COPYING*" -or -iname "*LICENSE*" |
      grep -v "\s" |
      egrep -v "meson.(test|msi|doc)" |
      egrep -v "(license.c|relicense.pl|license.doctree|LicenseCheck)"
    ) | tar -C share/licenses/qemu -xv

  # Add msys2 docs
  mkdir -pv share/doc/qemu
  cp -pv "${srcdir}"/msys2.readme.txt share/doc/qemu/
  cp -pv "${srcdir}"/msys2.examples.tests.sh share/doc/qemu/
}
